const path = require('path');
const { resolve } = require('path');
const autoprefixer = require('autoprefixer');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const miniCssExtractPlugin = require('mini-css-extract-plugin');


module.exports = {
    // 模式: 开发 生产
    mode: 'development' ,// production
    // source-map
    devtool: 'source-map',
    // 优化 ，禁止压缩 最小化
    optimization: {
        minimize: false
    },
    entry: {
        index: resolve(__dirname, './src/js/index.js'),
        detail: resolve(__dirname,'./src/js/detail.js'),
        collections: resolve(__dirname,'./src/js/collections.js')
    },
    output: {
        path: resolve(__dirname, './dist'),
        filename: 'js/[name].js'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                }
            },
            {
                test: /\.tpl$/,
                loader: 'ejs-loader',
                options: {
                    // variable: 'data',
                    // exportAsESM: true
                    esModule: false
                  }
            },
            {
                test: /\.css$/,
                use: [
                    'style-loader',
                    'css-loader',
                    {
                        loader: 'postcss-loader',
                        options: {
                            // plugins: function(){
                            //     return [autoprefixer('last 5 version')]
                            // }
                            postcssOptions: {
                                plugins: [
                                    autoprefixer({
                                        overrideBrowserslist: [
                                            "> 5%",
                                            'last 5 versions'
                                        ]
                                    })
                                ]
                            }
                        }
                    }
                ]
            },
            {
                test: /\.scss$/,
                use: [
                    // {
                    //     loader: miniCssExtractPlugin.loader,
                    //     options: {
                    //         hmr: process.env.NODE_ENV === "development"
                    //     }
                    // },
                    'style-loader',
                    // 'css-loader',
                    {
                        loader: 'css-loader',
                        options: {
                            importLoaders: 2,
                            url: false
                        }
                    },
                    {
                        loader: 'postcss-loader',
                        options: {
                            // plugins: function(){
                            //     return [autoprefixer('last 5 version')]
                            // }

                            postcssOptions: {
                                plugins: [
                                    autoprefixer({
                                        overrideBrowserslist: [
                                            "> 5%",
                                            "last 2 versions"
                                        ]
                                    })
                                ]
                            }
                        }
                    },
                    'sass-loader'
                ]
            },
            {
                test: /\.(png|jpg|jpeg|gif|ico|woff|eot|svg|ttf)$/i,
                loader: 'url-loader',
                options: {
                    limit: 1024,
                    name: 'img/[name].[hash:6].[ext]',
                    esModule: false
                }
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            filename: 'index.html',
            template: resolve(__dirname, './src/index.html'),
            title: '新闻头条',
            chunks: ['index'],
            chunksSortMode: 'manual', // 按照chunks 的顺序引入js文件
            excludeChunks: ['node-modules'] , // 排除node-modules 文件
            hash: true, // chunks 引入的时候加入hash 值
            minify: { // 去掉注释 换行和空格
                removeComments: true,
                collapseWhitespace: true
            }
        }),
        new HtmlWebpackPlugin({
            filename: 'detail.html',
            template: resolve(__dirname, './src/detail.html'),
            title: '新闻详情',
            chunks: ['detail'],
            chunksSortMode: 'manual', // 按照chunks 的顺序引入js文件
            excludeChunks: ['node-modules'] , // 排除node-modules 文件
            hash: true, // chunks 引入的时候加入hash 值
            minify: { // 去掉注释 换行和空格
                removeComments: true,
                collapseWhitespace: true
            }
        }),
        new HtmlWebpackPlugin({
            filename: 'collections.html',
            template: resolve(__dirname, './src/collections.html'),
            title: '我的新闻',
            chunks: ['collections'],
            chunksSortMode: 'manual', // 按照chunks 的顺序引入js文件
            excludeChunks: ['node-modules'] , // 排除node-modules 文件
            hash: true, // chunks 引入的时候加入hash 值
            minify: { // 去掉注释 换行和空格
                removeComments: true,
                collapseWhitespace: true
            }
        }),
    ],
    devServer: {
        static: {
            directory: path.join(__dirname, '/src')
        },
        open: true,
        host: 'localhost',
        port: '3000'
    },
    watchOptions: {
        ignored: /node_modules/,
    },
}